package com.hh.base.widget.common

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.hh.base.R
import com.hh.base.databinding.CellLayoutBinding

/**
 * Create By hHui on 2025/4/10 9:04
 *
 * @description 单元格
 */
class Cell : LinearLayout {
    private lateinit var viewBinding: CellLayoutBinding

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context, attrs, defStyle
    ) {
        initText(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initText(context, attrs)
    }

    init {
        initView()
    }

    private fun initText(context: Context, attrs: AttributeSet) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Cell)
        val isTopShow = typedArray.getBoolean(R.styleable.Cell_isTopLineShow, false)
        val isBottomShow = typedArray.getBoolean(R.styleable.Cell_isBottomLineShow, false)
        val isHaveNextPage = typedArray.getBoolean(R.styleable.Cell_haveNextPage, true)
        var mText = ""
        if (typedArray.getText(R.styleable.Cell_itemText) != null) {
            mText = typedArray.getText(R.styleable.Cell_itemText).toString()
        }
        val textColor = typedArray.getColor(R.styleable.Cell_itemTextColor, context.resources.getColor(R.color.black))
        typedArray.recycle() //释放资源
        viewBinding.txtItem.text = mText
        viewBinding.txtItem.setTextColor(textColor)
        viewBinding.lineBottom.visibility = if (isBottomShow) View.VISIBLE else View.INVISIBLE
        viewBinding.lineTop.visibility = if (isTopShow) View.VISIBLE else View.INVISIBLE
        viewBinding.imgRight.visibility = if (isHaveNextPage) View.VISIBLE else View.GONE
    }


    private fun initView() {
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.cell_layout, this, true)
    }
}